import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {ApiUrl} from "../../../../config/api-url";
import {UserInfoModel} from "../../../../models/user-info.model";
import {BaseService} from "../../../../shared/service/base.service";
import {Consts} from "../../../../config/consts";
import {NzFormatEmitEvent} from "ng-zorro-antd/tree";

@Component({
  selector: 'app-permission-parent',
  templateUrl: './permission-parent.component.html',
  styleUrls: ['./permission-parent.component.less']
})
export class PermissionParentComponent implements OnInit {

  menu;
  searchValue = '';
  nodes = [{
    key: 0,
    title: '一级菜单'
  }];

  @Input()
  isVisible = false;
  @Input()
  permissionId: string;
  @Input()
  platformType: any;

  @Output()
  emit: EventEmitter<any> = new EventEmitter();
  userInfo: UserInfoModel;

  constructor(private httpService: BaseService) {
    this.userInfo = JSON.parse(sessionStorage.getItem(Consts.USER_INFO));
  }

  ngOnInit(): void {
    // this.nodes = [{key: 0, title: '一级菜单'}];
    // this.getSimpleMenus();
  }

  ngOnChanges() {
    if(this.isVisible == true) {
      this.nodes = [{key: 0, title: '一级菜单'}];
      this.getSimpleMenus();
    }
  }

  handleOk(): void {
    this.isVisible = false;
    let body = {'isVisible': this.isVisible, 'menu': this.menu};
    this.emit.emit(body);
  }

  handleCancel(): void {
    this.isVisible = false;
    this.emit.emit(this.isVisible);
  }

  // 点击选中
  nzEvent(event: NzFormatEmitEvent): void {
    console.log(event);
    if (null != event.node) {
      this.menu = event.node.origin;
    }
  }

  public getSimpleMenus() {
    this.nodes = [{key: 0, title: '一级菜单'}];
    this.httpService.get(ApiUrl.PermissionUrl + '/menus?permissionId=' + this.permissionId + "&platformType=" + this.platformType).subscribe(
      data => {
        if (data.status == 0) {
          this.nodes = this.nodes.concat(data.data);
        }
      }
    );
  }

}
